package player 
{
	import collisions.Body;
	import collisions.LinkedList;
	import collisions.QuadTree;
	import enemies.Enemy;
	import entities.Entity;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Gareth Williams
	 */
	public class Player extends Entity
	{
		public var weaponStrength:int = 10; //this could be changed by pickups
		public var controller:IController = null;
		public function Player(x:Number = 0, y:Number = 0, radius:Number = 10) 
		{
			super(x, y, radius);
		}
		override public function destructor():void 
		{
			super.destructor();
		}
		override public function update():void //Entity
		{
			if (isAlive == false) return;
			
			var padDirection:Point = controller.getDirection();
			var padRotation:Number = controller.getAngle();
			
			this.x += padDirection.x * this.speed;
			this.y += padDirection.y * this.speed;
			
			this.viewCone.angleInRadians = this.rotation = padRotation - Constants.HALF_PI;
		}
		public function receiveDamage(amount:int):void
		{
			this.percentHealth -= amount;
			this.graphic.doHitAnimation();
			this.graphic.doHealthAnimation(percentHealth);
			if (true == checkWhetherDead())
			{
				//this.isAlive = false; for the purpose of the demo
				this.graphic.doDeathAnimation();
			}
		}
		override public function processProximityTo(collider:Body):void //Body
		{
			//implement view cone
			var delta:Point = Point(collider).subtract(this);
			var angleToChild:Number = Math.atan2(delta.y, delta.x);
			
			//unit vectors
			var aX:Number = Math.cos(viewCone.angleInRadians);
			var aY:Number = Math.sin(viewCone.angleInRadians);
			var bX:Number = Math.cos(angleToChild);
			var bY:Number = Math.sin(angleToChild);
			
			var dotProduct:Number = (aX * bX) + (aY * bY);
			var aperture:Number = Math.cos(this.viewCone.fovInRadians * 0.5);
			
			//test orthogonal
			var isVisible:Boolean = Boolean(dotProduct >= aperture);
			var isFiring:Boolean = controller.getFireButton();
			if (isVisible && isFiring)
			{
				//other types of entities may exist later like crates...
				if (collider is Enemy) Enemy(collider).receiveDamage(weaponStrength);
			}
		}
		//helpers
		private function checkWhetherDead():Boolean
		{
			return percentHealth <= 0;
		}
	}
}